package com.example.socketdemo1;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zhanghui
 * @date 2021/12/6 9:08
 */
public class Server {
    public static void main(String[] args) throws IOException {
        int port = 9999;
        // 绑定端口启动服务
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("服务器已启动");
        // 接收客户端socket连接
        Socket socket = serverSocket.accept();
        System.out.println("客户端[" + socket.getRemoteSocketAddress() + "]已连接");
        // 获取输入流, 获取客户端输入
        InputStream inputStream = socket.getInputStream();
        byte[] buffer = new byte[1024];
        int readLength = 0;
        while ((readLength = inputStream.read(buffer)) != -1) {
            System.out.println("接收数据: " + new String(buffer, 0, readLength));
        }
    }
}
